package com.lc.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.demo.bean.*;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.*;
import com.lc.demo.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private StudentService studentService;

    @Autowired
    ClassgradeService classgradeService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    private SigninService signinService;

    @Autowired
    private  TeacherService teacherService;


    @PostMapping(value = "/login")
    public ResponseMessage login(@RequestBody Admin admin) {
        Admin adm = adminService.login(admin.getUsername(), admin.getPassword());
        if (adm != null) {
            return ResponseMessage.successWithMessage(adm.getUsername());
        } else {
            return ResponseMessage.errorWithMessage("用户名或密码错误");
        }
    }


    //院系增删改查
    @PostMapping(value = "/getDepartmentList")
    public ResponseMessage getDepartmentList(@RequestBody Map<String,Object> map) {
        Department department=new Department();

        //把一个map的数据拷贝到s2中
        BeanUtil.copyProperties2Bean(map,department);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");

        PageHelper.startPage(pageNum, pageSize);
        List<Department> departmentList = departmentService.getDepartmentList(department);
        PageInfo<Department> page = new PageInfo<>(departmentList);

        return ResponseMessage.successWithData(page);
    }


    //院系新增
    @PostMapping(value = "/addDepartment")
    public ResponseMessage addDepartment(@RequestBody Department department) {
        // String username = (String) session.getAttribute("loginUser");
        departmentService.addDepartment(department);
        return ResponseMessage.success();
    }


    //院系修改
    @PostMapping(value = "/updateDepartment")
    public ResponseMessage updateDepartment(@RequestBody Department department) {
        // String username = (String) session.getAttribute("loginUser");
        departmentService.updateDepartment(department);
        return ResponseMessage.success();
    }

    //院系删除
    @PostMapping(value = "/delDepartment")
    public ResponseMessage delDepartment(@RequestBody Department department) {
        // String username = (String) session.getAttribute("loginUser");
        departmentService.delDepartment(department);
        return ResponseMessage.success();
    }

    //教师信息增删改查
    @PostMapping(value = "/addTeacher")
    public ResponseMessage addTeacher(@RequestBody Teacher teacher) {
        teacherService.addTeacher(teacher);
        return  ResponseMessage.success();
    }

    //教师信息增删改查
    @PostMapping(value = "/delTeacher")
    public ResponseMessage delTeacher(@RequestBody Teacher teacher) {
        teacherService.delTeacher(teacher);
        return  ResponseMessage.success();
    }

    //修改教师
    @PostMapping(value = "/updateTeacher")
    public ResponseMessage updateTeacher(@RequestBody Teacher teacher) {
        teacherService.updateTeacher(teacher);
        return  ResponseMessage.success();
    }


    @PostMapping(value = "/getTeacherList")
    public ResponseMessage getTeacherList(@RequestBody Map<String,Object> map) {
        Teacher teacher = new Teacher();
        BeanUtil.copyProperties2Bean(map,teacher);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teacherList=teacherService.getTeacherList(teacher);
        PageInfo<Teacher> page = new PageInfo<>(teacherList);
        return ResponseMessage.successWithData(page);
    }


    //班级增删改查接口
    //班级列表
    @PostMapping(value = "/getClassGradeList")
    public ResponseMessage getClassGradeList(@RequestBody Map<String,Object> map) {
        // String username = (String) session.getAttribute("loginUser");
        Classgrade classgrade=new Classgrade();
        BeanUtil.copyProperties2Bean(map,classgrade);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Classgrade> classGradeList = classgradeService.getClassGradeList(classgrade);
        PageInfo<Classgrade> page = new PageInfo<>(classGradeList);

        return ResponseMessage.successWithData(page);
    }

    //班级新增
    @PostMapping(value = "/addClassGrade")
    public ResponseMessage addClassGrade(@RequestBody Classgrade classgrade) {
        // String username = (String) session.getAttribute("loginUser");
        classgradeService.addClassGrade(classgrade);
        return ResponseMessage.success();
    }


    //班级修改
    @PostMapping(value = "/updateClassGrade")
    public ResponseMessage updateClassGrade(@RequestBody Classgrade classgrade) {
        // String username = (String) session.getAttribute("loginUser");
        classgradeService.updateClassGrade(classgrade);
        return ResponseMessage.success();
    }

    //班级删除
    @PostMapping(value = "/delClassGrade")
    public ResponseMessage delClassGrade(@RequestBody Classgrade classgrade) {
        // String username = (String) session.getAttribute("loginUser");
        classgradeService.delClassGrade(classgrade);
        return ResponseMessage.success();
    }

    //查询学生列表
    @PostMapping(value = "/getStudentList")
    public ResponseMessage getStudentList(@RequestBody Map<String,Object> map) {
        Student student =new Student();
        BeanUtil.copyProperties2Bean(map,student);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentService.getStudentList();
        PageInfo<Student> page = new PageInfo<>(studentList);
        return ResponseMessage.successWithData(page);
    }


    @PostMapping(value = "/addStudent")
    public ResponseMessage addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return ResponseMessage.success();
    }


    @PostMapping(value = "/updateStudent")
    public ResponseMessage updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return ResponseMessage.success();
    }


    @PostMapping(value = "/findStudent")
    public ResponseMessage findStudent(@RequestBody Student student) {
        Student student1 = studentService.selectByUsername(student.getUsername());
        return ResponseMessage.successWithData(student1);
    }


    @PostMapping(value = "/delStudent")
    public ResponseMessage delStudent(@RequestBody Student student) {
        studentService.delStudentByUsername(student.getUsername());
        return ResponseMessage.success();
    }

    @Autowired
    private CourseService courseService;


    //查询课程
    @PostMapping(value = "/getCourseList")
    public ResponseMessage getCourseList(@RequestBody Map<String,Object> map) {

        Course course=new Course();
        BeanUtil.copyProperties2Bean(map,course);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Course> courseListc = courseService.getCourseList(course);
        PageInfo<Course> page = new PageInfo<>(courseListc);
        return ResponseMessage.successWithData(page);
    }

    //课程新增
    @PostMapping(value = "/addCourse")
    public ResponseMessage addCourse(@RequestBody Course course) {
        // String username = (String) session.getAttribute("loginUser");
        courseService.addCourse(course);
        return ResponseMessage.success();
    }

    //课程修改
    @PostMapping(value = "/updateCourse")
    public ResponseMessage updateCourse(@RequestBody Course course) {
        // String username = (String) session.getAttribute("loginUser");
        courseService.updateCourse(course);
        return ResponseMessage.success();
    }

    //删除课程
    @PostMapping(value = "/delCourse")
    public ResponseMessage delCourse(@RequestBody Course course) {
        // String username = (String) session.getAttribute("loginUser");
        courseService.delCourse(course);
        return ResponseMessage.success();
    }



    //查询签到人数
    @PostMapping(value = "/findSignPersonList")
    public ResponseMessage findSignPersonList(@RequestBody Map<String,Object> map) {

        Course course=new Course();
        BeanUtil.copyProperties2Bean(map,course);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<SignPerson> signPersonList = courseService.findSignPerson(course);
        PageInfo<SignPerson> page = new PageInfo<>(signPersonList);
        return ResponseMessage.successWithData(page);
    }

    //生成本星期课表
    @GetMapping(value = "/createWeekSignin")
    public ResponseMessage createWeekSignin() {
        // String username = (String) session.getAttribute("loginUser");
        signinService.createWeekSignin();
        return ResponseMessage.success();
    }

    //删除本星期课表
    @GetMapping(value = "/deleteWeekSignin")
    public ResponseMessage deleteWeekSignin() throws ParseException {
        // String username = (String) session.getAttribute("loginUser");
        signinService.deleteWeekSignin();
        return ResponseMessage.success();
    }


    //查询全校课表
    @PostMapping(value = "/getAllSignin")
    public ResponseMessage getAllSignin(@RequestBody Map<String,Object> map) {
        Signin signin=new Signin();
        BeanUtil.copyProperties2Bean(map,signin);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Signin> signinList = signinService.getAllSignin();
        PageInfo<Signin> page = new PageInfo<>(signinList);
        return ResponseMessage.successWithData(page);
    }


    //查询全校课表  院系排名  班级排名 学生排名 三级联动
    @GetMapping(value = "/getRanking")
    public ResponseMessage getRanking() {
        List<Map<String,Object>> ranking = signinService.getRanking();
        return ResponseMessage.successWithData(ranking);
    }


    //重点监控   超过五次没有签到的学生
    @GetMapping(value = "/getKeyMonitoringStudent")
    public ResponseMessage getKeyMonitoringStudent() {
        Map<String,Object> returnMap = signinService.getKeyMonitoringStudent();
        return ResponseMessage.successWithData(returnMap);
    }

    //根据学生Id 查询出学生未签到的详情
    @PostMapping(value = "/getStudentSign")
    public ResponseMessage getStudentSign(@RequestBody Student student) {
      List<Signin> signinList = signinService.getStudentSign(student);
        return ResponseMessage.successWithData(signinList);
    }


}
